///*
// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
// *
// *  Redistribution and use in source and binary forms, with or without
// *  modification, are permitted provided that the following conditions are met:
// *
// *  Redistributions of source code must retain the above copyright notice,
// *  this list of conditions and the following disclaimer.
// *  Redistributions in binary form must reproduce the above copyright
// *  notice, this list of conditions and the following disclaimer in the
// *  documentation and/or other materials provided with the distribution.
// *  Neither the name of the dreamlu.net developer nor the names of its
// *  contributors may be used to endorse or promote products derived from
// *  this software without specific prior written permission.
// *  Author: Chill 庄骞 (smallchill@163.com)
// */
//package com.plian.system.service.fr.evaluation.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.fr.evaluation.AssetEvaluationA;
//import com.plian.system.entity.fr.evaluation.AssetEvaluationB;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.search.SearchParamEntiy;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.service.fr.evaluation.IAssetEvaluationAService;
//import com.plian.system.service.fr.evaluation.IAssetEvaluationBService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.fr.evaluation.AssetEvaluationAVO;
//import com.plian.system.wrapper.fr.evaluation.AssetEvaluationAWrapper;
//import lombok.AllArgsConstructor;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 资产评估项目审理主表 服务实现类
// *
// * @author jianglei
// * @since 2019-10-17
// */
//@Service
//@AllArgsConstructor
//public class AssetEvaluationAServiceImpl extends BaseServiceImpl<AssetEvaluationAMapper, AssetEvaluationA> implements IAssetEvaluationAService, BaseFlowService {
//
//    private IAssetEvaluationBService assetEvaluationBService;
//
//	private FormWorkflowService formWorkflowService;
//
//	private IFormCodeService formCodeService;
//
//	private AssetEvaluationAWrapper assetEvaluationAWrapper;
//
//	@Override
//    public boolean saveOrUpdate(AssetEvaluationA assetEvaluationA) {
//		if (assetEvaluationA.getId() == null){
//			assetEvaluationA.setVersion("1");
//			assetEvaluationA.setCode(formCodeService.getCode(FormTypeConstant.FR_ASSETEVALUATION_FORM));
//			assetEvaluationA.setFormType(String.valueOf(FormTypeConstant.FR_ASSETEVALUATION_FORM));
//		}
//        return super.saveOrUpdate(assetEvaluationA);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//		for (Long id : ids) {
//			AssetEvaluationA assetEvaluationA = getById(id);
//			if (assetEvaluationA == null) {
//				return false;
//			} else if (assetEvaluationA.getStatus() != null &&
//					(assetEvaluationA.getStatus() != FormStatusConstant.STASH && assetEvaluationA.getStatus() != FormStatusConstant.UNREVIEWED)) {
//				return false;
//			}
//			formWorkflowService.deleteProcessInstance(String.valueOf(id));
//		}
//		boolean result = super.deleteLogic(ids);
//		assetEvaluationBService.deleteLogicByAssetEvaluationId(ids);
//		return result;
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//		//将需要提交的流程变量返回，用于工作流侧
//		return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//		/**
//		 * 设置审批人和审批时间
//		 */
//		AssetEvaluationA assetEvaluationA = getById(id);
//
//		Date now = DateUtil.now();
//		assetEvaluationA.setApprovedTime(now);
//		updateById(assetEvaluationA);
//
//		changeBStatus(id);
//
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//	/**
//	 * 将子表数据状态置成已读
//	 * @param id
//	 */
//	private void changeBStatus(String id){
//		LambdaQueryWrapper<AssetEvaluationB> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.eq(AssetEvaluationB::getAssetEvaluationId, id);
//		List<AssetEvaluationB> assetEvaluationBS = assetEvaluationBService.list(queryWrapper);
//		if (CollectionUtil.isNotEmpty(assetEvaluationBS)){
//			for (AssetEvaluationB assetEvaluationB : assetEvaluationBS){
//				if (assetEvaluationB.getStatus() != 3){
//					assetEvaluationB.setStatus(3);
//					assetEvaluationBService.saveOrUpdate(assetEvaluationB);
//				}
//			}
//		}
//	}
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//		changeBStatus(id);
//
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//	@Override
//	public Boolean checkIsExisted(Long id, String code) {
//		if (Optional.ofNullable(code).isPresent()) {
//			AssetEvaluationA assetEvaluationA = new AssetEvaluationA();
//			assetEvaluationA.setCode(code);
//			if (Optional.ofNullable(getOne(Condition.getQueryWrapper(assetEvaluationA))).isPresent()) {
//				if (id==null || !id.equals(getOne(Condition.getQueryWrapper(assetEvaluationA)).getId())){
//					return false;
//				}
//			}
//		}
//		return true;
//	}
//
//	@Override
//	@Transactional(rollbackFor=RuntimeException.class)
//	public Boolean submit(AssetEvaluationA assetEvaluationA) {
//		if (!Optional.ofNullable(assetEvaluationA.getStatus()).isPresent()){
//			assetEvaluationA.setStatus(FormStatusConstant.UNREVIEWED);
//		}
//
//		boolean result = saveOrUpdate(assetEvaluationA);
//		List<AssetEvaluationB> assetEvaluationBS = assetEvaluationA.getAssetEvaluationBS();
//		if (CollectionUtil.isNotEmpty(assetEvaluationBS)){
//			/**
//			 * 资产评估项目审理子表
//			 */
//			List<Long> delIds = assetEvaluationBS.stream().filter(assetEvaluationB ->
//				Optional.ofNullable(assetEvaluationB.getIsDeleted()).isPresent() && assetEvaluationB.getIsDeleted() == 1)
//				.map(AssetEvaluationB::getId).collect(Collectors.toList());
//			if (CollectionUtil.isNotEmpty(delIds)){
//				assetEvaluationBService.deleteLogic(delIds);
//			}
//
//			assetEvaluationBS.removeIf(assetEvaluationB -> (Optional.ofNullable(assetEvaluationB.getIsDeleted()).isPresent() && assetEvaluationB.getIsDeleted() == 1));
//
//			if (CollectionUtil.isNotEmpty(assetEvaluationBS)) {
//				/**
//				 * 给新增数据赋值
//				 */
//				assetEvaluationBS.stream().filter(assetEvaluationB -> assetEvaluationB.getId() == null)
//					.forEach(auditContact -> {
//						auditContact.setAssetEvaluationId(assetEvaluationA.getId());
//						auditContact.setStatus(FormStatusConstant.UNREVIEWED);
//						auditContact.setIsDeleted(0);
//						auditContact.setCreateUser(TokenUtil.getTokenUserId());
//						auditContact.setCreateTime(new Date());
//					});
//				for (AssetEvaluationB assetEvaluationB : assetEvaluationBS){
//					assetEvaluationBService.saveOrUpdate(assetEvaluationB);
//				}
//
//			}
//		}
//
//		HashMap<String, Object> processVariable = new HashMap<>();
//		formWorkflowService.startProcessAndSimulate(String.valueOf(assetEvaluationA.getId()),assetEvaluationA.getFormType(),processVariable);
//
//		return result;
//	}
//	@Override
//	public java.io.File exportExcel(JSONObject jsonObject) {
//		List<SearchEntity> entityList = Optional.ofNullable(jsonObject.getJSONArray("query"))
//			.map(x -> x.toJavaList(SearchEntity.class)).orElse(new ArrayList<>());
//		QueryWrapper<AssetEvaluationA> qw= SearchParamEntiy.getQueryWrapper(
//			Condition.getQueryWrapper(new AssetEvaluationA()), entityList
//		);
//
//		List<AssetEvaluationA> floors = list(qw);
//		return null;
////		return ExcelUtils.exportExcelByList(jsonObject, AssetEvaluationAWrapper.build().listVO(floors));
//	}
//
//	@Override
//	public List<PageData> findByIds(List<String> formIds) {
//		QueryWrapper<AssetEvaluationA> queryWrapper = new QueryWrapper<>();
//		queryWrapper.in("id", Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//		List<AssetEvaluationA> vehicleLeases = list(queryWrapper);
//		List<AssetEvaluationAVO> vehicleLeaseVOS = assetEvaluationAWrapper.entityToVO(vehicleLeases);
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(vehicleLeaseVOS)){
//				for (AssetEvaluationAVO vo : vehicleLeaseVOS){
//					PageData pd = new PageData();
//					pd.put("id", vo.getId());
//					pd.put("UNITNAME", vo.getReportCompanyName());
//					pd.put("APPLICANTNAME", vo.getCreateUserName());
//					pd.put("applicantId", vo.getCreateUser());
//					pd.put("applyOrganizationId", vo.getReportCompanyId());
//					pd.put("formType", FormTypeConstant.FR_ASSETEVALUATION_FORM);
//					pd.put("orgName", vo.getReportCompanyName());
//					pageDataList.add(pd);
//				}
//			}
//
//			return pageDataList;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return pageDataList;
//	}
//
//	@Override
//	public List<Integer> getFormType() {
//		return Arrays.asList(FormTypeConstant.FR_ASSETEVALUATION_FORM);
//	}
//
//	@Override
//	public void goBackToStash(String formId) {
//		handleStatus(Long.valueOf(formId), UNREVIEWED);
//	}
//
//	@Override
//	public Boolean change(AssetEvaluationA assetEvaluationA) {
//		AssetEvaluationA old = getById(assetEvaluationA.getId());
//		AssetEvaluationB assetEvaluationB = new AssetEvaluationB();
//		assetEvaluationB.setAssetEvaluationId(old.getId());
//		old.setAssetEvaluationBS(assetEvaluationBService.list(Condition.getQueryWrapper(assetEvaluationB)));
//
//		//		snapshotClient.push("assetEvaluationA", old.getId(), jsonObject);
//		//assetEvaluationA.setStatus(BillStatus.INIT.billStatus());
//		assetEvaluationA.setVersion(StringUtil.isBlank(assetEvaluationA.getVersion()) ?
//			"1" : String.valueOf(Integer.valueOf(assetEvaluationA.getVersion())+1));
//		return submit(assetEvaluationA);
//	}
//
//	/**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            AssetEvaluationA assetEvaluationA = getById(id);
////            if (assetEvaluationA.getStatus() != judgeStatus) {
////                 return false;
////             }
//			assetEvaluationA.setStatus(updateStatus);
//            updateById(assetEvaluationA);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//        }
